<?php

if (!defined('UC_FILESYSTEM_ROOT')) 
{
    define('UC_FILESYSTEM_ROOT', dirname(__FILE__) . '/');
    require_once UC_FILESYSTEM_ROOT . 'UC_PHPFileSystemAutoloader.php';
}

abstract class UC_PHPFileSystemElement
{
    public function __construct($path)
    {
        if(is_string($path))
        {
            if(file_exists($path))
            {
                $this->path = $path;
            }
            else
            {
                throw new UC_PHPFileSystemException(
                    "The path does not exist : $path");
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'The path should be a string');
        }
    }
    
    public function copy($destination)
    {
        if(!copy($this->get_path(), $destination))
        {
            throw new UC_PHPFileSystemException(
                'Failed to copy file or directory.');
        }
    }
    
    abstract public function delete();
    
    public function move($destination)
    {
        if(rename($this->get_path(), $destination))
        {
            $this->path = $destination;            
        }
        else
        {
            throw new UC_PHPFileSystemException(
                'Failed to move file or directory');
        }
    }
    
    public function get_date_created()
    {
        if(file_exists($this->path))
        {
            return filectime($this->path);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }

    public function get_date_last_accessed()
    {
        if(file_exists($this->path))
        {
            return fileatime($this->path);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }

    public function get_date_last_modified()
    {
        if(file_exists($this->path))
        {
            return filemtime($this->path);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }
    
    public function get_extension()
    {
        if(file_exists($this->path))
        {
            return pathinfo($this->path, PATHINFO_EXTENSION);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }

    public function get_name()
    {
        if(file_exists($this->path))
        {
            return pathinfo($this->path, PATHINFO_FILENAME);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }
    
    public function set_name($name)
    {
        if(is_string($name))
        {
            if(!strpbrk($name, "\\/?%*:|\"<>"))
            {
                rename($this->get_path(), 
                    $this->get_parent_directory() . '/' . $name);
                
                $this->path = $this->get_parent_directory() . '/' . $name;
            }
            else
            {
                throw new InvalidArgumentException(
                    'Invalid character in file name.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'The name should be a string');
        }
    }

    public function get_parent_directory()
    {
        if(file_exists($this->path))
        {
            return dirname($this->path);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }

    public function get_path()
    {
        if(file_exists($this->path))
        {
            return $this->path;
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }

    public function get_size()
    {
        if(file_exists($this->path))
        {
            return filesize($this->path);
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Not found: {$this->path}");
        }
    }
    
    protected $path;
}

?>
